﻿using Microsoft.Office.Interop.Excel;
using RAE.Logica.Info;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RAE.Logica.ExcelLogic
{
    class ExcelExport
    {
        public static void exportList(List<Ajedrecista> lista, string path)
        {
            try
            {
                Ajedrecista[] listCopy = new Ajedrecista[lista.Count];
                lista.CopyTo(listCopy);
                Array.Sort(listCopy);

                Microsoft.Office.Interop.Excel.Application WRExcel = null;
                Microsoft.Office.Interop.Excel.Workbooks WRwbs = null;
                Microsoft.Office.Interop.Excel._Workbook WRwb = null;
                Microsoft.Office.Interop.Excel.Sheets WRss = null;
                Microsoft.Office.Interop.Excel._Worksheet WRws = null;
                object objOpt = System.Reflection.Missing.Value;

                //create Excel spreadsheet
                WRExcel = new Microsoft.Office.Interop.Excel.Application();
                WRwbs = (Microsoft.Office.Interop.Excel.Workbooks)WRExcel.Workbooks;
                WRwb = (Microsoft.Office.Interop.Excel.Workbook)(WRwbs.Add(objOpt));
                WRss = (Microsoft.Office.Interop.Excel.Sheets)WRwb.Worksheets;
                WRws = (Microsoft.Office.Interop.Excel._Worksheet)(WRss.get_Item(1));

                int row = 1;
                //create Excel column headings
                WRws.Cells[row, 1] = "Rank";
                WRws.Cells[row, 2] = "Título";
                WRws.Cells[row, 3] = "Nombre";
                WRws.Cells[row, 4] = "Rating";

                foreach (Ajedrecista a in lista)
                {
                    int i = Array.IndexOf(listCopy, a);
                    Ajedrecista b = listCopy[i];
                    string titulos = "";
                    foreach (Titulo t in b.titulos)
                        if (titulos != "")
                            titulos = titulos + ',' + t.codigo.ToString();
                        else titulos = t.codigo.ToString();

                    row++;
                    WRws.Cells[row, 1] = i + 1;
                    WRws.Cells[row, 2] = titulos;
                    WRws.Cells[row, 3] = b.ToString();
                    WRws.Cells[row, 4] = b.puntaje_elo.ToString();
                }

                //save workbook
                WRExcel.DisplayAlerts = false;
                Microsoft.Office.Interop.Excel.Dialog dialog = WRExcel.Dialogs[Microsoft.Office.Interop.Excel.XlBuiltInDialog.xlDialogSaveAs];
                if (dialog.Show(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault))
                {
                    //close workbook
                    WRwbs.Close();

                    MessageBox.Show("Archivo guardado satisfactoriamente.");
                }
                WRExcel.DisplayAlerts = true;
            }
            catch (Exception e)
            {
                MessageBox.Show("El archivo no fue guardado.");
            }
        }
    }
}
